package com.weipai.education.controller;

import com.weipai.education.model.ApplicationUser;
import com.weipai.education.model.User;
import com.weipai.education.model.api.Response;
import com.weipai.education.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Author: 曹朋飞
 * @Date: 2018/12/10 15:52
 */
@RestController
@Validated
public class UserController {
    
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @param userCateId
     * @param key
     * @param type:如果为null查询所有经销商，如果为own查询自己下面的经销商
     * @return
     */
    @RequestMapping(value = "/user/list", method = RequestMethod.GET)
    public Response list(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                         @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                         @RequestParam(value = "userCateId", required = false) Integer userCateId,
                         @RequestParam(value = "key", required = false) String key,
                         @RequestParam(value = "type", required = false) String type){
        return userService.list(page, size, userCateId, key, type);
    }
/**
     * 分页查询
     * @param page
     * @param size
     * @param userCateId
     * @param key
     * @param type:如果为null查询所有经销商，如果为own查询自己下面的经销商
     * @return
     */
    @RequestMapping(value = "/user/Mylist", method = RequestMethod.GET)
    public Response Mylist(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                         @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                         @RequestParam(value = "userCateId", required = false) Integer userCateId,
                         @RequestParam(value = "key", required = false) String key,
                         @RequestParam(value = "type", required = false) String type){
        return userService.Mylist(page, size, userCateId, key, type);
    }

    /**
     * 市级经销商查询他所有的下级
     * @param page
     * @param size
     * @param userCateId
     * @param key
     * @return
     */
    @RequestMapping(value = "/user/inventoryUserList", method = RequestMethod.GET)
    public Response inventoryUserList(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                                      @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                                      @RequestParam(value = "userCateId", required = false) Integer userCateId,
                                      @RequestParam(value = "key", required = false) String key){
        return userService.inventoryUserList(page, size, userCateId, key);
    }

    /**
     * 经销商保存--修改
     * @param user
     * @return
     */
    @RequestMapping(value = "/user/save", method = RequestMethod.POST)
    public Response save(@Valid User user){
        return userService.save(user);
    }

    /**
     * 账号的保存修改
     * @param mobile
     * @param roleId
     * @param password
     * @return
     */
    @RequestMapping(value = "/user/userSave", method = RequestMethod.POST)
    public Response userSave(@NotBlank(message = "手机号不能为空") String mobile,
                             @NotNull(message = "角色id不能为空") Integer roleId,
                             @NotBlank(message = "密码不能为空") String password,
                             @NotBlank(message = "昵称不能为空") String nickname,
                             @RequestParam(value = "id", required = false) String id){
        return userService.userSave(mobile, roleId, password, nickname, id);
    }

    /**
     * 更改经销商的状态
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/changeStatus", method = RequestMethod.GET)
    public Response changeStatus(@NotBlank(message = "经销商id不能为空") String id){
        return userService.changeStatus(id);
    }

    /**
     * 审核经销商
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/isAudit", method = RequestMethod.GET)
    public Response isAudit(@NotBlank(message = "经销商id不能为空") String id, 
                            @NotBlank(message = "审核状态不能为空") String isAudit){
        return userService.isAudit(id, isAudit);
    }

    /**
     * 批量删除
     * @param id:
     * @return
     */
    @RequestMapping(value = "/user/batchDeleteById", method = RequestMethod.GET)
    public Response batchDeleteById(@NotBlank(message = "经销商id不能为空") String[] id){
        return userService.batchDeleteById(id);
    }

    /**
     * 申请经销商
     * @param applicationUser
     * @return
     */
    @RequestMapping(value = "/user/applicationUser", method = RequestMethod.POST)
    public Response applicationUser(@Valid ApplicationUser applicationUser){
        return userService.applicationUser(applicationUser);
    }

    /**
     * 加盟商申请列表
     * @param page
     * @param size
     * @param userCateId
     * @param key
     * @param type:type为null查询全部的，如果type==own表示查询自己的
     * @return
     */
    @RequestMapping(value = "/user/applicationUserList", method = RequestMethod.GET)
    public Response applicationUserList(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                                           @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                                           @RequestParam(value = "userCateId", required = false) Integer userCateId,
                                           @RequestParam(value = "key", required = false) String key,
                                           @RequestParam(value = "isAudit", required = false) String isAudit,
                                           @RequestParam(value = "type", required = false) String type){
        return userService.applicationUserList(page, size, userCateId, key, isAudit, type);
    }

    /**
     * 根据用户名检索用户
     * @param type:1表示模糊查询用户，2表示此账号是否可用
     * @param accountNumber
     * @return
     */
    @RequestMapping(value = "/user/searchAccountNumber", method = RequestMethod.GET)
    public Response searchAccountNumber(@NotBlank(message = "type不能为空") String type,
                                        @NotBlank(message = "账号不能为空") String accountNumber){
        return userService.searchAccountNumber(type, accountNumber);
    }

    /**
     * 权限账号列表
     * @param page
     * @param size
     * @param roleId
     * @param key
     * @return
     */
    @RequestMapping(value = "/user/userList", method = RequestMethod.GET)
    public Response userList(@NotNull(message = "page不能为空") @RequestParam(value = "page") Integer page,
                             @NotNull(message = "size不能为空") @RequestParam(value = "size") Integer size,
                             @RequestParam(value = "roleId", required = false) Integer roleId,
                             @RequestParam(value = "key", required = false) String key){
        return userService.userList(page, size, roleId, key);
    }

    /**
     * 更改密码--后端
     * @param newPassword
     * @param newTwoPassword
     * @return
     */
    @RequestMapping(value = "/user/updatePassword", method = RequestMethod.POST)
    public Response updatePasswordPc(@NotBlank(message = "用户id不能为空") String userId,
                                     @NotBlank(message = "新密码不能为空") String newPassword,
                                     @NotBlank(message = "二次输入密码不能为空") String newTwoPassword){
        return userService.updatePasswordPc(userId, newPassword, newTwoPassword);
    }
    
}
